<?php

class CategorieDAO extends DAO{


	public function getById($id){
		try
		{
			$id = Validator::escapeSql($id);
			$query="select * from CATEGORIES where id=$id;";
			$statement=$this->pdo->query($query);
			$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
			$docDAO = $this->factory->getDocumentDAO();
			$catInfoDAO = $this->factory->getCategorieInfoDAO();

			$thebean = null;
			foreach($array_obj as $Bean){
				 
				$thebean = new Categorie((int)$Bean->ID);
				 
				$thebean->setDefaultDocument($this->getDefaultDocument($thebean));
				 
				$thebean->setParentCategorieId((int)$Bean->PARENT_CAT_ID);
				 
				$thebean->setRootFlag((int)$Bean->IS_ROOT);
				 
				$thebean->setChildDocuments($docDAO->getAllByCategorie($thebean));
				 
				$thebean->setInfos($catInfoDAO->getAllByCategorie($thebean));

				$thebean->setChildCategories($this->getAllByCategorie($thebean));

			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $thebean;
	}
	function getDefaultDocument($Categorie){
		if(is_a($Categorie, "Categorie")){
			$docDAO = $this->factory->getDocumentDAO();
			$doc = $docDAO->getDefaultByCategorie($Categorie);

			return $doc;
		}
	}
	function remove($Bean){
		try{
			$docDAO = $this->factory->getDocumentDAO();
			$catInfDAO = $this->factory->getCategorieInfoDAO();
			foreach ($Bean->getChildCategories() as $Cat){
				$this->remove($Cat);
			}
			foreach ($Bean->getChildDocuments() as $Doc){
				$docDAO->remove($Doc);
			}
			foreach ($Bean->getInfos() as $Inf){
				$catInfDAO->remove($Inf);
			}
			$query="delete from CATEGORIES where id='".Validator::escapeSql($Bean->getId())."';";
			$this->pdo->exec($query);
			$bool=true;
		}catch(Exception $e)    {
			$bool=false;
		}
		return $bool;

	}
	function update($Bean){
		try{
			$catInfDAO = $this->factory->getCategorieInfoDAO();
			foreach ($Bean->getInfos() as $Inf){
				$catInfDAO->update($Inf);
			}
			$query = "update CATEGORIES set parent_cat_id=".Validator::escapeSql($Bean->getParentCategorieId())." ,IS_ROOT = ".Validator::escapeSql($Bean->isRoot())." where id=".Validator::escapeSql($Bean->getId()).";";
			$this->pdo->exec($query);
			$bool=true;
		}
		catch(Exception $e)     {
			$bool=false;

		}
		return $bool;
	}
	function add($Bean){
		try{
			$catInfDAO = $this->factory->getCategorieInfoDAO();
			foreach ($Bean->getInfos() as $Inf){
				$catInfDAO->add($Inf);
			}
			$insertQuery = "insert into CATEGORIES (PARENT_CAT_ID,IS_ROOT) values (".Validator::escapeSql($Bean->getParentCategorieId()).",".Validator::escapeSql($Bean->isRoot()).");";
			$thebean = null;
			$this->pdo->exec($insertQuery);
			$thebean = $this->getById((int)$this->pdo->lastInsertId());

		}catch(Exception $e)    {$this->pdo->message;}
		return $thebean;
	}
	function getAllByCategorie($Categorie){
		try
		{
			if(is_a($Categorie, 'Categorie')){
				 
				$BeanArray = array();
				$query="select * from CATEGORIES where parent_cat_id=".Validator::escapeSql($Categorie->getId()).";";

				$statement=$this->pdo->query($query);
				$array_obj=$statement->fetchAll(PDO::FETCH_OBJ);
				$thebean = null;
				foreach($array_obj as $Bean){

					$thebean = $this->getById((int)$Bean->ID);
					array_push($BeanArray,$thebean);
				}
			}
		}
		catch(Exception $e)     {
			throw $e;
		}

		return $BeanArray;

	}
}
?>